﻿using System.Text;
using System.Web.UI;
using System.Xml.Linq;

namespace SPAdaptiveMenu.Controls
{
    ///<summary>
    ///</summary>
    public class MenuOption : MenuItem
    {
        ///<summary>
        ///</summary>
        public string OnClickText { get; set; }

        public override MenuItemType ItemType
        {
            get { return MenuItemType.Option; }
        }

        ///<summary>
        ///</summary>
        public MenuOption()
        {
            Disabled = false;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            var sb = new StringBuilder();
            sb.Append("<ie:menuitem");
            sb.AppendFormat(@" id=""{0}""", ClientID);
            sb.Append(@" type=""option""");

            if (!string.IsNullOrEmpty(OnClickText))
                sb.AppendFormat(@" onMenuClick=""{0}; return false;""", OnClickText);
            if (!string.IsNullOrEmpty(Title))
                sb.AppendFormat(@" title=""{0}""", Title);
            if (!string.IsNullOrEmpty(IconToolTip))
                sb.AppendFormat(@" iconAltText=""{0}""", IconToolTip);
            if (!string.IsNullOrEmpty(IconSrc))
                sb.AppendFormat(@" iconSrc=""{0}""", IconSrc);
            if (!string.IsNullOrEmpty(Description))
                sb.AppendFormat(@" description=""{0}""", Description);
            if (!string.IsNullOrEmpty(Title))
                sb.AppendFormat(@" text=""{0}""", Title);
            if (Disabled)
                sb.Append(@" enabled=""false""");
            if (Hidden)
                sb.Append(@" hidden=""true""");
            sb.Append("></ie:menuitem>");
            writer.Write(sb.ToString());
        }

        protected internal override XElement Serialized
        {
            get
            {
                var element = base.Serialized;
                element.Add(new XAttribute("Type", ItemType));
                element.Add(new XAttribute("OnClickText", OnClickText ?? string.Empty));
                return element;
            }
        }

        protected override object SaveControlState()
        {
            base.SaveControlState();
            var obj = base.SaveControlState();
            return obj != null
                ? (object)new Pair(obj, Serialized.ToString())
                : Serialized.ToString();
        }

        protected override void LoadControlState(object state)
        {
            if (state == null) return;
            var p = state as Pair;
            if (p != null)
            {
                base.LoadControlState(p.First);
                InitProperties(XElement.Parse((string)p.Second));
            }
            else
            {
                if (state is string)
                    InitProperties(XElement.Parse((string)state));
                else
                    base.LoadControlState(state);
            }
        }

        protected internal override void InitProperties(XElement element)
        {
            if (element == null) return;
            base.InitProperties(element);
            OnClickText = element.Attribute("OnClickText").Value;
        }

        protected internal override XElement HTML
        {
            get
            {
                var element = base.HTML;
                element.Add(new XAttribute("type", "option"));
                element.Add(new XAttribute("onMenuClick", OnClickText ?? string.Empty));
                return element;
            }
        }
    }
}
